﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace PuzzleCoop
{
    public class Board
    {
        public static Point Size = new Point(10, 10);

        public Slot[,] map = new Slot[Size.X, Size.Y];
        PlayScreen playScreen;

        public Vector2 DrawPositionA = Vector2.Zero;
        public Vector2 DrawPositionE = Vector2.Zero;

        Texture2D blackScreen;


        public Board(PlayScreen playScreen)
        {
            blackScreen = GameScreenManager.Load<Texture2D>("Images\\BlackScreen");
            this.playScreen = playScreen;
            Random r = new Random(new Random(DateTime.Now.Millisecond).Next(DateTime.Now.Second));

            for (int i = 0; i < Size.X; ++i)
                for (int j = 0; j < Size.Y; ++j)
                {
                    int a = r.Next(10);
                    if (a < 4)
                        map[i, j] = new EmptySlot(new Point(i, j));
                    else if (a == 4)
                        map[i, j] = new Wall(new Point(i, j));
                    else
                    { map[i, j] = new Block(new Point(i, j), null); GenerateDen(i, j); }
                }
            map[0, 0] = new Block(new Point(0, 0), new bool[4] { true, true, true, true });
        }

        public void GenerateDen(int i, int j)
        {
            Vector2[] pos = new Vector2[4] { new Vector2(100, 100), new Vector2(100, 400), new Vector2(400, 100), new Vector2(400, 400) };
            for (int k = 0; k < 4; ++k)
                if (Rand.Next(4) == 0) 
                    playScreen.gameObjects.Add(new Den(playScreen, new Vector2(i, j) * Slot.SizeA + pos[k]));
        }

        public void Update(GameTime gameTime)
        {
            DrawPositionA = playScreen.player.drawPosition - playScreen.player.position + new Vector2(Slot.SizeA / 2, Slot.SizeA / 2);
            DrawPositionE = playScreen.player.DrawPositionE - new Vector2(playScreen.player.CoordE.X * Slot.SizeE, playScreen.player.CoordE.Y * Slot.SizeE) + new Vector2(Slot.SizeE / 2, Slot.SizeE / 2);
        }

        public void DrawA(SpriteBatch spriteBatch)
        {
            // PA
            // desenha apenas os slots em volta do personagem
            Point p = GetCoord(playScreen.player.position);
            for (int i = p.X - 1; i < p.X + 2; ++i)
                for (int j = p.Y - 1; j < p.Y + 2; ++j)
                    if (i >= 0 && j >= 0 && i < Size.X && j < Size.Y)
                        map[i, j].DrawA(spriteBatch, DrawPositionA);
        }

        public void DrawE(SpriteBatch spriteBatch)
        {
            // desenha uma tela preta no lado do PE pra esconder os lixos da parte do PA
            spriteBatch.Draw(blackScreen, new Vector2(1280 / 2, 0), Color.White);

            // PE
            // desenha todos os Slots
            Point p = playScreen.player.CoordE;
            for (int i = p.X - 6; i < p.X + 7; ++i)
                for (int j = p.Y - 6; j < p.Y + 7; ++j)
                    if (i >= 0 && j >= 0 && i < Size.X && j < Size.Y)
                        map[i, j].DrawE(spriteBatch, DrawPositionE);
        }

        public Slot GetSlot(Vector2 pos)
        {
            Point p = GetCoord(pos);
            if (p.X < 0 || p.Y < 0 || p.X >= Size.X || p.Y >= Size.Y) return null;
            return map[p.X, p.Y];
        }

        public Point GetCoord(Vector2 pos)
        {
            return new Point((int)(pos.X / Slot.SizeA), (int)(pos.Y / Slot.SizeA));
        }

    }
}
